Skip to content

fix(sentry): move cross-request static state into coroutine context - #1086

Open
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r7
Open

fix(sentry): move cross-request static state into coroutine context#1086
huangdijia wants to merge 1 commit into
mainfrom
sentry-pr/r7

Conversation

@huangdijia

Copy link
Copy Markdown
Contributor

问题

src/sentry 中存在两处进程级静态可变状态,在 Swoole 协程并发下会互相覆盖并跨请求残留:

  1. Integration::$transaction(私有静态属性)——getTransaction()setupOnce() 注册的事件处理器中被读取,决定事件上的 transaction 名;协程 A 写入后协程 B 覆盖,导致事件携带错误的 transaction,且值在请求结束后仍驻留进程。
  2. Constants::$runningInCommand(公共静态属性)——被 OnBeforeHandle 写入、OnMetricFactoryReady 读取,同样为进程级共享,协程/请求间相互污染。

修改

将两处静态属性改为基于 Hyperf\Context\Context 的协程级存储(随协程生命周期自动回收):

  • Integration.php:删除 private static ?string $transaction,新增 CONTEXT_TRANSACTION 常量;getTransaction()/setTransaction() 方法签名与可见性不变,内部改为读写 Context。
  • Constants.php:删除公共静态属性 public static bool $runningInCommand,新增 CTX_RUNNING_IN_COMMAND 常量及静态方法 runningInCommand(): boolsetRunningInCommand(bool $running = true): void
  • OnBeforeHandle.php:Constants::$runningInCommand = trueConstants::setRunningInCommand()
  • OnMetricFactoryReady.php:! SentryConstants::$runningInCommand! SentryConstants::runningInCommand()

⚠️ BC:公共静态属性 Constants::$runningInCommand 被同名静态方法 Constants::runningInCommand() 替代,直接读写该属性的外部代码需要改用方法调用(读取 → runningInCommand(),写入 → setRunningInCommand())。Integration::$transaction 原为私有属性,无外部 BC。

测试

新增 tests/Sentry/CoroutineScopedStateTest.php(PHPUnit 风格类,经 FriendsOfHyperf\Tests\TestCaseRunTestsInCoroutine trait 在 Swoole\Coroutine::run() 内执行),共 4 个用例:

  • 协程内 setTransaction('A')getTransaction() === 'A',setTransaction(null)null;
  • 两个协程通过 Channel 同步:各自读写互不干扰(协程 1 仍读到 'A');
  • runningInCommand() 默认 false,setRunningInCommand() 后为 true,可再次关闭;
  • runningInCommand 两个协程间隔离(协程 2 未设置时为 false)。

验证

  • vendor/bin/pest --group=sentry(sentry 组全部用例,含新增 4 例):通过(47 passed / 83 assertions);
  • php-cs-fixer fix --dry-run --diff 改动文件:0 处需修复;
  • git diff --check:无空白错误;
  • phpstan analyse src/sentry:No errors;
  • 新增测试在旧实现(静态属性)下会失败(setRunningInCommand() 不存在 / 协程间互相读到对方值),可有效防止回归。

@coderabbitai

coderabbitai Bot commented Aug 20, 2026

Copy link
Copy Markdown

Warning

Review limit reached

@huangdijia, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 58 minutes

Limit details: You’ve used all 2 included reviews currently available.

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

How can I continue?

Wait for the limit to reset, then comment @coderabbitai review or push new commits to the PR.

An organization admin can change what happens after included review limits in Billing.

How do review limits work?

CodeRabbit enforces per-developer PR review limits within each organization.

For paid Pro and Pro+ reviews, CodeRabbit uses a developer's included PR review attempts over the past 7 days to set the current hourly allowance. At typical activity levels, the full plan allowance applies. Higher sustained activity can lower the allowance until earlier attempts leave the 7-day window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 55757ed2-4f2b-4298-ace5-0313c71c2c19

📥 Commits

Reviewing files that changed from the base of the PR and between c814785 and afe36a5.

📒 Files selected for processing (5)
  • src/sentry/src/Constants.php
  • src/sentry/src/Integration.php
  • src/sentry/src/Metrics/Listener/OnBeforeHandle.php
  • src/sentry/src/Metrics/Listener/OnMetricFactoryReady.php
  • tests/Sentry/CoroutineScopedStateTest.php

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant